/*
	Simple Software Renderer (SSRenderer)
	Copyright (C) 2007 Piotr Orzechowski <piotr.orzechowski@gmail.com>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
*/

inline void SSRRenderer::ClearColorBuffer(const color32 &color)
{
	///@todo moze istnieje jakies szybkie systemowe wypelnianie int'ami ?
	register u32 size = m_pColorSurface->GetPitch() * m_pColorSurface->GetHeight();
	register u32 argb = color;

    for(register u32 i = 0; i < size; ++i)
	{
		m_pColorBuffer[i].argb = argb;
	}
}

inline void SSRRenderer::PutPixel(u32 x, u32 y, const color32 &color)
{
	m_pColorBuffer[x + y * m_ColorBufferPitch] = color;
}

inline void SSRRenderer::BlendPixel_Alpha_0_5(u32 x, u32 y, const color32 &color)
{
	color32 *dst = m_pColorBuffer + x + y * m_ColorBufferPitch;

	dst->r = (dst->r >> 1) + (color.r >> 1);
	dst->g = (dst->g >> 1) + (color.g >> 1);
	dst->b = (dst->b >> 1) + (color.b >> 1);
}

inline void SSRRenderer::BlendPixel_Alpha_0_25(u32 x, u32 y, const color32 &color)
{
	color32 *dst = m_pColorBuffer + x + y * m_ColorBufferPitch;

	// dst->R -= (dst->R / 4) - (color.r / 4) so
	// dst->R = dst->R - (dst->R / 4) + (color.r / 4) [X]

	dst->r -= (dst->r >> 2) - (color.r >> 2);
	dst->g -= (dst->g >> 2) - (color.g >> 2);
	dst->b -= (dst->b >> 2) - (color.b >> 2);
}

inline void SSRRenderer::BlendPixel_Alpha_0_125(u32 x, u32 y, const color32 &color)
{
	color32 *dst = m_pColorBuffer + x + y * m_ColorBufferPitch;

	dst->r -= (dst->r >> 3) - (color.r >> 3);
	dst->g -= (dst->g >> 3) - (color.g >> 3);
	dst->b -= (dst->b >> 3) - (color.b >> 3);
}

inline color32 SSRRenderer::GetPixel(u32 x, u32 y)
{
	return m_pColorBuffer[x + y * m_ColorBufferPitch];
}
